<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- advtutorial.qdoc -->
  <title>QML Advanced Tutorial 1 - Creating the Game Canvas and Blocks | Qt Quick 5.12.3</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.12</a></td><td ><a href="qtquick-index.html">Qt Quick 模块</a></td><td ><font style="vertical-align: inherit;">QML 高级教程 1 - 创建游戏画布和块</font></td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtquick-index.html">Qt 5.12.3 参考指南</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
  <link rel="prev" href="qml-advtutorial.html" />
  <link rel="next" href="qtquick-tutorials-samegame-samegame2-example.html" />
<p class="naviNextPrevious headerNavi">
<a class="prevPage" href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtquick/qml-advtutorial.html"><font style="vertical-align: inherit;">QML 高级教程</font></a>
<span class="naviSeparator">  &#9702;  
<font style="vertical-align: inherit;"><a class="nextPage" href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtquick/qtquick-tutorials-samegame-samegame2-example.html">QML 高级教程 2 - 填充游戏画布</a></font></span>
</p><p/>
<div class="sidebar">
<div class="toc">
<h3><a name="toc">目录</a></h3>
<ul>
<li class="level2"><a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtquick/qtquick-tutorials-samegame-samegame1-example.html#creating-the-application-screen"><font style="vertical-align: inherit;">创建应用程序屏幕</font></a></li>
<li class="level2"><a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtquick/qtquick-tutorials-samegame-samegame1-example.html#adding-button-and-block-components"><font style="vertical-align: inherit;">添加</font><code>Button</code><font style="vertical-align: inherit;">和</font><code>Block</code><font style="vertical-align: inherit;">组件</font></a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title"><font style="vertical-align: inherit;">QML 高级教程 1 - 创建游戏画布和块</font></h1>
<span class="subtitle"></span>
<!-- $$$tutorials/samegame/samegame1-description -->
<div class="descr"> <a name="details"></a>
<a name="creating-the-application-screen"></a>
<h3 ><font style="vertical-align: inherit;">创建应用程序屏幕</font></h3>
<p><font style="vertical-align: inherit;">第一步是在您的应用程序中创建基本的 QML 项。</font></p>
<p><font style="vertical-align: inherit;">首先，我们创建具有如下主屏幕的 Same Game 应用程序：</font></p>
<p class="centerAlign"><img src="images/declarative-adv-tutorial1.png" alt="" /></p><p><font style="vertical-align: inherit;">这是由主应用程序文件定义的，</font><code>samegame.qml</code><font style="vertical-align: inherit;">如下所示：</font></p>
<pre class="qml">

  import QtQuick 2.0

  <span class="type"><a href="qml-qtquick-rectangle.html">Rectangle</a></span> {
      <span class="name">id</span>: <span class="name">screen</span>

      <span class="name">width</span>: <span class="number">490</span>; <span class="name">height</span>: <span class="number">720</span>

      <span class="type"><a href="qml-qtquick-systempalette.html">SystemPalette</a></span> { <span class="name">id</span>: <span class="name">activePalette</span> }

      <span class="type"><a href="qml-qtquick-item.html">Item</a></span> {
          <span class="name">width</span>: <span class="name">parent</span>.<span class="name">width</span>
          <span class="type">anchors</span> { <span class="name">top</span>: <span class="name">parent</span>.<span class="name">top</span>; <span class="name">bottom</span>: <span class="name">toolBar</span>.<span class="name">top</span> }

          <span class="type"><a href="qml-qtquick-image.html">Image</a></span> {
              <span class="name">id</span>: <span class="name">background</span>
              <span class="name">anchors</span>.fill: <span class="name">parent</span>
              <span class="name">source</span>: <span class="string">&quot;../shared/pics/background.jpg&quot;</span>
              <span class="name">fillMode</span>: <span class="name">Image</span>.<span class="name">PreserveAspectCrop</span>
          }
      }

      <span class="type"><a href="qml-qtquick-rectangle.html">Rectangle</a></span> {
          <span class="name">id</span>: <span class="name">toolBar</span>
          <span class="name">width</span>: <span class="name">parent</span>.<span class="name">width</span>; <span class="name">height</span>: <span class="number">30</span>
          <span class="name">color</span>: <span class="name">activePalette</span>.<span class="name">window</span>
          <span class="name">anchors</span>.bottom: <span class="name">screen</span>.<span class="name">bottom</span>

          <span class="type"><a href="../qtquickcontrols1/qml-qtquick-controls-button.html">Button</a></span> {
              <span class="type">anchors</span> { <span class="name">left</span>: <span class="name">parent</span>.<span class="name">left</span>; <span class="name">verticalCenter</span>: <span class="name">parent</span>.<span class="name">verticalCenter</span> }
              <span class="name">text</span>: <span class="string">&quot;New Game&quot;</span>
              <span class="name">onClicked</span>: <span class="name">console</span>.<span class="name">log</span>(<span class="string">&quot;This doesn't do anything yet...&quot;</span>)
          }

          <span class="type"><a href="qml-qtquick-text.html">Text</a></span> {
              <span class="name">id</span>: <span class="name">score</span>
              <span class="type">anchors</span> { <span class="name">right</span>: <span class="name">parent</span>.<span class="name">right</span>; <span class="name">verticalCenter</span>: <span class="name">parent</span>.<span class="name">verticalCenter</span> }
              <span class="name">text</span>: <span class="string">&quot;Score: Who knows?&quot;</span>
          }
      }
  }

</pre>
<p><font style="vertical-align: inherit;">这为您提供了一个基本的游戏窗口，其中包括块的主画布、“新游戏”按钮和分数显示。</font></p>
<p><font style="vertical-align: inherit;">您可能在这里不认识的一项是</font><a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtquick/qml-qtquick-systempalette.html"><font style="vertical-align: inherit;">SystemPalette</font></a><font style="vertical-align: inherit;">项。这提供了对 Qt 系统调色板的访问，并用于为按钮提供更原生的外观。</font></p>
<p><code>Item</code><font style="vertical-align: inherit;">请注意,</font><code>Button</code><font style="vertical-align: inherit;">和类型的锚点</font><code>Text</code><font style="vertical-align: inherit;">是使用组（点）表示法设置的，以提高可读性。</font></p>
<a name="adding-button-and-block-components"></a>
<h3 ><font style="vertical-align: inherit;">添加</font><code>Button</code><font style="vertical-align: inherit;">和</font><code>Block</code><font style="vertical-align: inherit;">组件</font></h3>
<p>上面代码中的Button项定义在一个名为<code>Button.qml</code> 的独立组件文件中。为了创建一个功能按钮，我们在<a href="qml-qtquick-rectangle.html">Rectangle</a>内使用了QML类型<a href="qml-qtquick-text.html">Text</a>和<a href="qml-qtquick-mousearea.html">MouseArea</a>。下面是<code>Button.qml</code> 
代码:</p>
<pre class="qml">

  import QtQuick 2.0

  <span class="type"><a href="qml-qtquick-rectangle.html">Rectangle</a></span> {
      <span class="name">id</span>: <span class="name">container</span>

      property <span class="type"><a href="../qtqml/qml-string.html">string</a></span> <span class="name">text</span>: <span class="string">&quot;Button&quot;</span>

      signal <span class="type">clicked</span>

      <span class="name">width</span>: <span class="name">buttonLabel</span>.<span class="name">width</span> <span class="operator">+</span> <span class="number">20</span>; <span class="name">height</span>: <span class="name">buttonLabel</span>.<span class="name">height</span> <span class="operator">+</span> <span class="number">5</span>
      <span class="type">border</span> { <span class="name">width</span>: <span class="number">1</span>; <span class="name">color</span>: <span class="name">Qt</span>.<span class="name">darker</span>(<span class="name">activePalette</span>.<span class="name">button</span>) }
      <span class="name">antialiasing</span>: <span class="number">true</span>
      <span class="name">radius</span>: <span class="number">8</span>

      <span class="comment">// color the button with a gradient</span>
      <span class="name">gradient</span>: <span class="name">Gradient</span> {
          <span class="type"><a href="qml-qtquick-gradientstop.html">GradientStop</a></span> {
              <span class="name">position</span>: <span class="number">0.0</span>
              <span class="name">color</span>: {
                  <span class="keyword">if</span> (<span class="name">mouseArea</span>.<span class="name">pressed</span>)
                      <span class="keyword">return</span> <span class="name">activePalette</span>.<span class="name">dark</span>
                  <span class="keyword">else</span>
                      <span class="keyword">return</span> <span class="name">activePalette</span>.<span class="name">light</span>
              }
          }
          <span class="type"><a href="qml-qtquick-gradientstop.html">GradientStop</a></span> { <span class="name">position</span>: <span class="number">1.0</span>; <span class="name">color</span>: <span class="name">activePalette</span>.<span class="name">button</span> }
      }

      <span class="type"><a href="qml-qtquick-mousearea.html">MouseArea</a></span> {
          <span class="name">id</span>: <span class="name">mouseArea</span>
          <span class="name">anchors</span>.fill: <span class="name">parent</span>
          <span class="name">onClicked</span>: <span class="name">container</span>.<span class="name">clicked</span>();
      }

      <span class="type"><a href="qml-qtquick-text.html">Text</a></span> {
          <span class="name">id</span>: <span class="name">buttonLabel</span>
          <span class="name">anchors</span>.centerIn: <span class="name">container</span>
          <span class="name">color</span>: <span class="name">activePalette</span>.<span class="name">buttonText</span>
          <span class="name">text</span>: <span class="name">container</span>.<span class="name">text</span>
      }
  }

</pre>
<p>这本质上定义了一个包含文本并且可以点击的矩形。MouseArea有一个<code>onClicked()</code> 
处理程序，该处理程序实现在区域被单击时发出<code>container</code> 的 <code>clicked()</code> 信号。</p>
<p><font style="vertical-align: inherit;">在同一游戏中，当游戏开始时，屏幕上会充满小块。每个块只是一个包含图像的项目。块代码在一个单独的</font><code>Block.qml</code><font style="vertical-align: inherit;">文件中定义：</font></p>
<pre class="qml">

  import QtQuick 2.0

  <span class="type"><a href="qml-qtquick-item.html">Item</a></span> {
      <span class="name">id</span>: <span class="name">block</span>

      <span class="type"><a href="qml-qtquick-image.html">Image</a></span> {
          <span class="name">id</span>: <span class="name">img</span>
          <span class="name">anchors</span>.fill: <span class="name">parent</span>
          <span class="name">source</span>: <span class="string">&quot;../shared/pics/redStone.png&quot;</span>
      }
  }

</pre>
<p><font style="vertical-align: inherit;">目前，该块没有做任何事情。它只是一个图像。随着教程的进行，我们将为块设置动画并赋予其行为。我们还没有添加任何代码来创建块；我们将在下一章中这样做。</font></p>
<p><font style="vertical-align: inherit;">我们使用 . 将图像设置为其父项的大小</font><code>anchors.fill: parent</code><font style="vertical-align: inherit;">。这意味着当我们稍后在教程中动态创建和调整块项目时，图像将自动缩放到正确的大小。</font></p>
<p><font style="vertical-align: inherit;">注意 Image 类型</font><code>source</code><font style="vertical-align: inherit;">属性的相对路径。此路径相对于包含</font><a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtquick/qml-qtquick-image.html"><font style="vertical-align: inherit;">Image</font></a><font style="vertical-align: inherit;">类型的文件的位置。或者，您可以将图像源设置为绝对文件路径或包含图像的 URL。</font></p>
<p><font style="vertical-align: inherit;">到目前为止，您应该熟悉代码。我们刚刚创建了一些基本类型来开始使用。接下来，我们将用一些块填充游戏画布。</font></p>
<p>文件：</p>
<ul>
<li><a href="qtquick-tutorials-samegame-samegame1-block-qml.html">tutorials/samegame/samegame1/Block.qml</a></li>
<li><a href="qtquick-tutorials-samegame-samegame1-button-qml.html">tutorials/samegame/samegame1/Button.qml</a></li>
<li><a href="qtquick-tutorials-samegame-samegame1-samegame-qml.html">tutorials/samegame/samegame1/samegame.qml</a></li>
<li><a href="qtquick-tutorials-samegame-samegame1-samegame1-qmlproject.html">tutorials/samegame/samegame1/samegame1.qmlproject</a></li>
</ul>
</div>
<!-- @@@tutorials/samegame/samegame1 -->
<p class="naviNextPrevious footerNavi">
<a class="prevPage" href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtquick/qml-advtutorial.html"><font style="vertical-align: inherit;">QML 高级教程</font></a>
<span class="naviSeparator">  &#9702;  
<font style="vertical-align: inherit;"><a class="nextPage" href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtquick/qtquick-tutorials-samegame-samegame2-example.html">QML 高级教程 2 - 填充游戏画布</a></font></span>
</p>
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
